﻿using BeetleX;
using BeetleX.Clients;
using JT808.Protocol;
using JT808.Protocol.Interfaces;
using JT808.Protocol.MessageBody;
using JT809.Protocol;
using JT809.Protocol.Enums;
using JT809.Protocol.Extensions;
using JT809.Protocol.Interfaces;
using JT809.Protocol.MessageBody;
using JT809.Protocol.Metadata;
using JT809.Protocol.SubMessageBody;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;

namespace TcpTest
{
    class Program
    { 
       static IServiceProvider ServiceProvider;
          static  JT809Serializer JT809_2019_Serializer;
           static IJT809Config JT809_2019_Config;
        static JT808Serializer JT808Serializer_2019;
        static Program()
        {
            IServiceCollection serviceDescriptors = new ServiceCollection();
            serviceDescriptors.AddJT809Configure(new JT809_2019_Config());

            ServiceProvider = serviceDescriptors.BuildServiceProvider();

            JT809_2019_Config = ServiceProvider.GetRequiredService<JT809_2019_Config>();
            JT809_2019_Serializer = JT809_2019_Config.GetSerializer();
            IJT808Config jT808Config = new DefaultGlobal808_2019Config();
            JT808Serializer_2019 = new JT808Serializer(jT808Config);
        }
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            #region 定位消息
            //JT808_0x0200 jT808UploadLocationRequest = new JT808_0x0200
            //{
            //    AlarmFlag = 1,
            //    Altitude = 40,
            //    GPSTime = DateTime.Parse("2018-07-15 10:10:10"),
            //    Lat = 12222222,
            //    Lng = 132444444,
            //    Speed = 60,
            //    Direction = 0,
            //    StatusFlag = 2,
            //    JT808LocationAttachData = new Dictionary<byte, JT808_0x0200_BodyBase>()
            //};
            //jT808UploadLocationRequest.JT808LocationAttachData.Add(JT808Constants.JT808_0x0200_0x01, new JT808_0x0200_0x01
            //{
            //    Mileage = 100
            //});
            //jT808UploadLocationRequest.JT808LocationAttachData.Add(JT808Constants.JT808_0x0200_0x02, new JT808_0x0200_0x02
            //{
            //    Oil = 55
            //});
            //var jt808_0x0200Hex = JT808Serializer_2019.Serialize(jT808UploadLocationRequest, JT808.Protocol.Enums.JT808Version.JTT2019);



            JT809_0x1200 jT809_0X1200 = new JT809_0x1200();
            jT809_0X1200.VehicleColor = JT809VehicleColorType.黑色;
            jT809_0X1200.VehicleNo = "粤A66666";
            jT809_0X1200.SubBusinessType = JT809SubBusinessType.实时上传车辆定位信息.ToUInt16Value();
            jT809_0X1200.SubBodies = new JT809_0x1200_0x1202()
            {
                //GNSSData = new JT809VehiclePositionProperties_2019
                //{
                //    GnssData = jt808_0x0200Hex,
                //    PlatformId1 = "11111111111",
                //    Alarm1 = 1,
                //    PlatformId2 = "22222222222",
                //    Alarm2 = 2,
                //    PlatformId3 = "33333333333",
                //    Alarm3 = 3,
                //}
                VehiclePosition = new JT809VehiclePositionProperties
                {
                    Day = (byte)(DateTime.Now.Day),
                    Month = (byte)(DateTime.Now.Month),
                    Year = (ushort)(DateTime.Now.Year),
                    Hour = (byte)(DateTime.Now.Hour),
                    Minute = (byte)(DateTime.Now.Minute),
                    Second = (byte)(DateTime.Now.Second),
                    Alarm = 1,
                    Direction = 2,
                    State = 2,
                    Altitude = 32,
                    Lat = 122334565,
                    Lon = 12354563,
                    Vec1 = 112,
                    Vec2 = 22,
                    Vec3 = 12
                }
            };




            JT809Package package = new JT809Package();
            //var package = JT809.Protocol.Enums.JT809BusinessType.主链路车辆动态信息交换业务.Create(jT809_0X1200);
            package.Header = new JT809Header
            {
                MsgGNSSCENTERID = 2020,
                BusinessType = JT809BusinessType.主链路车辆动态信息交换业务.ToUInt16Value(),
                Version = new JT809Header_Version(1, 0, 0)
            };

            package.Bodies = jT809_0X1200;
            var result = JT809_2019_Serializer.Serialize(package).ToHexString();
            Console.WriteLine(result);
            //主链路车辆动态信息交换业务 
            #endregion






            //JT809Package jT809Package = new JT809Package();
            //jT809Package.Header = new JT809Header
            //{
            //    BusinessType = JT809BusinessType.主链路登录请求消息.ToUInt16Value(),
            //    MsgGNSSCENTERID = 2020,
            //    Version = new JT809Header_Version(1, 0, 0),
            //    //Time = DateTime.Parse("2020-04-26 12:02:00")
            //};

            //JT809_0x1001 bodies = new JT809_0x1001();

            //bodies.UserId = 123456;
            //bodies.Password = "test809111";
            //bodies.DownLinkIP = "47.93.10.42";
            //bodies.MsgGNSSCENTERID = 2020;
            //bodies.DownLinkPort = 52181;
            //jT809Package.Bodies = bodies;

            //var hex = JT809_2019_Serializer.Serialize(jT809Package).ToHexString();

            //AsyncTcpClient client =SocketFactory.CreateClient<AsyncTcpClient>("47.93.10.42", 52181);
            //BytesHandler line = hex;
            //client.Send(line);
            //client.DataReceive = (o, e) =>
            //{
            //    var pipestream = e.Stream.ToPipeStream();
            //    if (pipestream.TryReadLine(out string line))
            //    {
            //        Console.WriteLine(line);
            //    }
            //};
            //while (true)
            //{
            //    //Console.Write("Enter Name:");
            //    //BytesHandler line = Console.ReadLine() + "\r\n";
            //    //client.Send(line);
            //    if (!client.IsConnected)
            //    {

            //        Console.WriteLine(client.LastError.Message);
            //    }
            //}




            var data = "5B000000EB00001D841200000007E401020F000001E240D4C142465933363500000000000000000000000000021203000000B505000F0307E50C272006CC79B8015A02CBF20000000000001ECC015A020032000C00C300000000000F0307E50C273406CC79B8015A02CBF20000000000001ECC015A020032000C00C300000000000F0307E50C280B06CC79B8015A02CBF20000000000001ECC015A020032000C00C300000000000F0307E50C282006CC79B8015A02CBF20000000000001ECC015A020032000C00C300000000000F0307E50C283406CC79B8015A02CBF20000000000001ECC015A020032000C00C30000000055425D".ToHexBytes();//5B 00 00 00 1F 00 00 1B 72 10 02 00 00 07 E4 01 00 00 00 00 00 00 00 00 00 0F 42 3F 30 2C 5D 
            JT809Package jT809Package = JT809_2019_Serializer.Deserialize(data);


            Console.WriteLine();



            //if (TcpClientBusiness.IsConnected)//发送登录请求
            //{


            //TcpClientBusiness.SendData(hex.ToHexBytes());
            //}










            //Console.WriteLine("Hello World!");
        }


        static void TcpLinster()
        {
            AsyncTcpClient client = SocketFactory.CreateClient<AsyncTcpClient>("47.93.10.42", 52181);
            client.DataReceive = (o, e) =>
            {
                var pipestream = e.Stream.ToPipeStream();
                if (pipestream.TryReadLine(out string line))
                {
                    Console.WriteLine(line);
                }
            };
            while (true)
            {
                //Console.Write("Enter Name:");
                //BytesHandler line = Console.ReadLine() + "\r\n";
                //client.Send(line);
                if (!client.IsConnected)
                {
                    Console.WriteLine(client.LastError.Message);
                }
            }
        }



       
    }


    public class JT809_2019_Config : JT809GlobalConfigBase
    {
        public override string ConfigId => "JT809_2011";

        public JT809_2019_Config()
        {
            Version = JT809Version.JTT2011;
        }
    }

    public class DefaultGlobal808_2019Config : GlobalConfigBase
    {
        public override string ConfigId { get; protected set; }
        public DefaultGlobal808_2019Config(string configId = "jt808_2019")
        {
            ConfigId = configId;

        }
    }
}
